package com.dcits.filter.sensitive;

import ch.qos.logback.classic.pattern.MessageConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class SensitiveDataConverter extends MessageConverter {
    private final Map<String, String> rules = Maps.newConcurrentMap();

    @Override
    public String convert(ILoggingEvent event) {
        String message = event.getFormattedMessage();
        // 实现脱敏逻辑
        return desensitize(message);
    }

    private String desensitize(String message) {
        // 自定义脱敏规则...
        final String str = System.getProperty(SensitiveDataConfig.SENSITIVE_DATA_KEY);
        try {
            if (str != null) {
                rules.putAll(JSON.parseObject(str, Map.class));
            }
        } catch (Exception e) {
            log.error("JSON Convert Error", e);
        }
        return SensitiveDataUtils.desensitize(message, rules);
    }
}